<?php
/**
 * Description of Loader
 *
 * @author dan
 */
class Loader {
    
    private $viewVariables = null;
    private $scripts = null;
    private $styles = null;

    public function __construct() {
        $this->viewVariables = array();
        $this->scripts = array();
        $this->styles = array();
        
    }
    
    
    /**
     * load a javascript file
     * 
     * @param string $script
     * @param int $priority 
     */
    function loadScript($script, $priority = 5){
        $this->scripts[] = array('script' => $script, 'priority' => $priority);
    }
    
    /**
     * loads a stylesheet
     * 
     * @param string $style
     * @param int $priority 
     */
    function loadStyle($style, $priority = 5){
        $this->styles[] = array('style' => $style, 'priority' => $priority);
    }
    
    /**
     *
     * Loads a view.
     * 
     * @param string $view - the name of the view to load.
     * @param array $vars - an array of variables to be passed to the view.
     * @param boolean $return - If true will return the view as string. 
     * false by default.
     * 
     * @throws ViewException
     * 
     * @return mixed - will return true once the view is loaded, or a 
     * string if the $return param was set to true
     */
    function loadView($view, $vars = null, $return = false) {
        $file = BASE_PATH . "/app/view/" . $view . ".php";
        
        if (is_array($vars) && !empty($vars)) {
            $this->addViewVariables($vars);
        }
        
        $moreVars = array();
        $moreVars['scripts'] = $this->getScripts();
        $moreVars['styles'] = $this->getStyles();
        
        if(is_array($vars)){
            array_merge($vars, $moreVars);
        }
        
        extract($this->viewVariables, EXTR_PREFIX_SAME, 'pre_');

        if (!file_exists($file)){
            throw new LoaderException("Unable to load view: $view");
        }

        if (!$return) {
            include ($file);
            return true;
        } else {
            ob_start();
            include ($file);
            $viewOutput = ob_get_contents();
            ob_end_clean();
            return $viewOutput;
        }
    }
    
    function helper($helper){
        $helper = strtolower($helper);
        
        $file = BASE_PATH . "/app/helpers/" . $helper . ".php";
        if(file_exists($file)){
            include($file);
            return true;
        }
        return false;
    }
    
    private function prioritySort($a, $b){
        if($a['priority'] == $b['priority']){
            return 0;
        }
        return ($a['priority'] < $b['priority']) ? -1 : 1;
    }
    
    private function getScripts(){
        $scriptArray = array();
        usort($this->scripts, array($this,'prioritySort'));
        foreach($this->scripts as $script){
            $scriptArray[] = $script['script'];
        }
        return $scriptArray;
    }
    
    private function getStyles(){
        $styleArray = array();
        usort($this->styles, array($this,'prioritySort'));
        foreach($this->styles as $style){
            $styleArray[] = $style['style'];
        }
        return $styleArray;
    }
    
}